<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/device.css">
</script>
<style scoped>
    .layui-fluid {
        padding: 20px;
    }

    .layui-btn {
        display: inline;
    }

    .ButtonGround {
        margin-bottom: 15px;
    }

    .ButtonGround button {
        color: #fff;
        border: 0px;
        background: #98AEBE;
    }
</style>
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-md12">
            <!-- 按钮组合 -->
            <div class="ButtonGround">
                <button type="button" class="layui-btn layui-btn-xs  layui-btn-radius layui-btn-primary Btn01"
                    data-type="xunJianButton">巡检计划自动生成工单设置</button>
                <button type="button" class="layui-btn layui-btn-xs  layui-btn-radius layui-btn-primary Btn02"
                    data-type="baoYangButton">保养计划自动生成工单设置</button>
            </div>
            <table id="table" class="layui-table" lay-filter="table"></table>
        </div>
    </div>
</div>
<script type="text/html" id="table-list">
    <p>{{ d.list }}</p>
</script>
<script>
    layui.use(['table', 'admin', 'laypage', 'jquery', 'laytpl', 'tree', 'form', 'api', 'treeSelect', 'laydate', 'utill', 'view'], function () {
        var table = layui.table,
            admin = layui.admin,
            laypage = layui.laypage,
            setter = layui.setter,
            utill = layui.utill,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            tree = layui.tree,
            treeSelect = layui.treeSelect,
            api = layui.api,
            view = layui.view,
            laydate = layui.laydate,
            form = layui.form;
            $('.Btn01').on({
            mouseover: function(){
                layer.tips('工单生成时间与是否自动生成设置', '.Btn01', {
                    tips: [1,'#333333'],
                    time:2000
                });
            }
            }) ;
            $('.Btn02').on({
            mouseover: function(){
                layer.tips('工单生成时间与是否自动生成设置', '.Btn02', {
                    tips: [1,'#333333'],
                    time:2000
                });
            }
            }) ;
        table.render(
            $.extend({}, utill.tableOpts(), {
                skin: 'nob',
                elem: '#table',
                method: 'POST',
                url: api.host + api.new.one,
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list  //解析数据列表
                    };
                },
                cols: [[
                      { field: 'ruleType', title: '名称' }
                    , { field: 'data', title: '规则', templet: '#table-list' },
                    { field: 'preview', title: '编号预览'}
                ]],
            })
        );
        table.on('row(table)', function (obj) {
            let id = obj.data.ruleId;
            sessionStorage.setItem('ruleId', JSON.stringify(id));
            $(this).attr('lay-href', '/home/details');
            var href = '[data-jump="home/user"]';
            sessionStorage.setItem('layui-this', JSON.stringify(href));
        });
        // 事件
        var active = {
            xunJianButton: function () {
                admin.popup({
                    title: '巡检工单',
                    area: ['392px', '230px'],
                    shadeClose: true,
                    id: 'LAY-popup-user-add',
                    success: function (layero, index) {
                        view(this.id).render('home/alert/xunJian').done(function () {
                            //监听提交
                            var obj = {
                                "trigger": "cron1"
                            }
                            admin.req({
                                url: api.host + api.task_selectTask_llp,
                                type: 'post',
                                contentType: 'application/json',
                                dataType: 'json',
                                data: JSON.stringify(obj),
                                done: function (res) {
                                    $("#xjTree").val(res.data.date.split(" ")[1])
                                    if (res.data.isStop == "NORMAL") {
                                        $("#isStop").attr('checked', 'checked');
                                        form.render();
                                    }
                                    // 关闭
                                    $(".btn2").click(function () {
                                        layer.close(layer.index);
                                    })
                                }
                            });
                            $(".btn1").click(function () {
                                var task = {
                                    "cron": $("#xjTree").val(),
                                    "job": "job1",
                                    "trigger": "cron1"
                                }
                                admin.req({
                                    url: api.host + api.task_update,
                                    type: 'post',
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify(task),
                                    done: function (res) {
                                        layer.msg(res.message);
                                        layer.close(index);
                                    }
                                });
                            })

                            $("#isStop1").click(function () {
                                var llp = $("#isStop").attr('checked') == 'checked';
                                var task = {};
                                if (llp == true) {
                                    task = {
                                        "job": "job1",
                                        "isStart": "NORMAL"
                                    }

                                } else {
                                    task = {
                                        "job": "job1",
                                        "isStart": "PAUSED"
                                    }
                                }
                                admin.req({
                                    url: api.host + api.stop_llp,
                                    type: 'post',
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify(task),
                                    done: function (res) {
                                        layer.msg(res.message);
                                        layer.close(index);
                                    }
                                });
                            })
                        });
                    }
                });
            },
            baoYangButton: function () {
                admin.popup({
                    title: '保养工单',
                    area: ['392px', '230px'],
                    shadeClose: true,
                    id: 'LAY-popup-user-add',
                    success: function (layero, index) {
                        view(this.id).render('home/alert/baoYang').done(function () {
                            //监听提交
                            //监听提交
                            var obj = {
                                "trigger": "cron2"
                            }
                            admin.req({
                                url: api.host + api.task_selectTask_llp,
                                type: 'post',
                                contentType: 'application/json',
                                dataType: 'json',
                                data: JSON.stringify(obj),
                                done: function (res) {
                                    $("#bYTree").val(res.data.date.split(" ")[1])
                                    if (res.data.isStop == "NORMAL") {
                                        $("#isStop").attr('checked', 'checked');
                                        form.render();
                                    }
                                    // 关闭
                                    $(".btn2").click(function () {
                                        layer.close(layer.index);
                                    })
                                }
                            });
                            $(".btn1").click(function () {
                                var task = {
                                    "cron": $("#bYTree").val(),
                                    "job": "job2",
                                    "trigger": "cron2"
                                }
                                admin.req({
                                    url: api.host + api.task_update,
                                    type: 'post',
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify(task),
                                    done: function (res) {
                                        layer.msg(res.message);
                                        layer.close(index);
                                    }
                                });
                            })

                            $("#isStop2").click(function () {
                                var llp = $("#isStop").attr('checked') == 'checked';
                                var task = {};
                                if (llp == true) {
                                    task = {
                                        "job": "job2",
                                        "isStart": "NORMAL"
                                    }

                                } else {
                                    task = {
                                        "job": "job2",
                                        "isStart": "PAUSED"
                                    }
                                }
                                admin.req({
                                    url: api.host + api.stop_llp,
                                    type: 'post',
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify(task),
                                    done: function (res) {
                                        layer.msg(res.message);
                                        layer.close(index);
                                    }
                                });
                            })
                            // 点击取消关闭弹窗
                            $(".btn2").click(function () {
                                layer.close(layer.index);
                            })
                        });
                    }
                });
            }
        }

        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    })
</script>